package org.diysearch.tools;

import java.io.StringWriter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.client.solrj.SolrServer;

import org.diysearch.annotations.SolrArray;
import org.diysearch.annotations.SolrArrayElement;
import org.diysearch.annotations.SolrField;
import org.diysearch.interfaces.Model;
import org.diysearch.interfaces.TemplateEngine;
import org.diysearch.models.SolrDocument;
import org.diysearch.models.SolrResult;

public abstract class SolrTools {
	private static final Log LOG = LogFactory.getLog(SolrTools.class);
	private static final String SOLR_URL = BaseTool.CONFIG_BEAN.getSolrUrl();
	private static SolrServer server = null;
	
	
	public static void addModel(Model m) throws Exception {
		Field[] fields = m.getClass().getDeclaredFields();
		List<SolrDocument> docs = new ArrayList<SolrDocument>();
		
		TemplateEngine te = (TemplateEngine) BaseTool.IOC_ENGINE.getBean("templateEngine");
		
		for (Field f : fields) {
			if (f.isAnnotationPresent(SolrField.class)) {
				String key = f.getAnnotation(SolrField.class).value();
				Object value = PropertyUtils.getProperty(m,f.getName());
				
				if (value instanceof List && f.isAnnotationPresent(SolrArray.class)) {
					for (Model im : ((List<Model>) value)) {
						Field[] _f = im.getClass().getDeclaredFields();
						for (Field _f0 : _f) {
							if (_f0.isAnnotationPresent(SolrArrayElement.class)) {
								SolrDocument sd = new SolrDocument();
								sd.setName(_f0.getAnnotation(SolrField.class).value());
								sd.setValue(PropertyUtils.getProperty(im,_f0.getName()).toString());
								docs.add(sd);
							}
						}
					}
				} else {
					SolrDocument sd = new SolrDocument();
					sd.setName(key);
					sd.setValue(value.toString());
					
					docs.add(sd);
				}
			}
		}
		Map<String,Object> content = new HashMap<String,Object>();
		content.put("fields",docs);
		StringWriter solrXml = (StringWriter) te.getContent("system/solr-add.ftl", content);
		String url = BaseTool.CONFIG_BEAN.getSolrUrl() + "/update";
		int code = HttpTools.doPost(url, solrXml.toString(), "text/xml", "utf-8");
		code = HttpTools.doPost(url, "<commit/>", "text/xml", "utf-8");
		
	}

	public static List<SolrResult> score (List<String> terms) throws Exception {
		List<SolrResult> results = new ArrayList<SolrResult>();
		
		for (String term: terms) {
			SolrResult sr = new SolrResult();
			sr.setTerm(term);
			StringBuffer sb = new StringBuffer();
			
			sb.append(SOLR_URL)
				.append("select/?q=")
				.append(term)
				.append(BaseTool.CONFIG_BEAN.getSolrQueryString());
			
			String resp = HttpTools.doGet(sb.toString());

			List<SolrResult> scores = BaseTool.CONFIG_BEAN.getSolrResponseHandler().process(resp);
			
			LOG.debug(resp);
			results.add(sr);
		}
		
		return results;
	}
	
}
